/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import com.google.gson.Gson;
import conexion.JsonFormat;
import conexion.SqlFormat;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import libreriaTicket.dao.DaoTipoEvento;
import libreriaTicket.dao.DaoTipoUbicacion;
import libreriaTicket.entity.TipoUbicacion;

/**
 *
 * @author CITT-2013
 */
public class SvTipoUbicacion extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here. You may use following sample code. 
             out.println("<!DOCTYPE html>");
             out.println("<html>");
             out.println("<head>");
             out.println("<title>Servlet svTipoUbicacion</title>");            
             out.println("</head>");
             out.println("<body>");
             out.println("<h1>Servlet svTipoUbicacion at " + request.getContextPath() + "</h1>");
             out.println("</body>");
             out.println("</html>");*/
        } finally {
            //out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);


        int accion = Integer.parseInt(request.getParameter("accion"));

        

        if (accion == SqlFormat.INSERTAR) {
            insertTipoUbicacion(request, response);
        }
        if (accion == SqlFormat.SELECCIONAR) {
            selectAll(request, response);
        }
        if(accion == SqlFormat.ELIMINAR){
            deleteTipoUbicacion(request, response);
        }
        if(accion == SqlFormat.ACTUALIZAR){
            updateTipoUbicacion(request, response);
        }
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

    public void insertTipoUbicacion(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("application/json");
        PrintWriter out = response.getWriter();
        
        
        TipoUbicacion tipoUbicacion = new TipoUbicacion();
        String nombre = request.getParameter("nombre");
        String descripcion = request.getParameter("descripcion");
        
        tipoUbicacion.setDescripcion(descripcion);
        tipoUbicacion.setNombre(nombre);
        
        //--Se devuelve el json 
        JsonFormat json = new JsonFormat();

        if (DaoTipoUbicacion.sqlInsert(tipoUbicacion)) {
            
            json.addField("codigo", 1);
        } else {
            json.addField("codigo", 0);
        }

    }
    public void updateTipoUbicacion(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("application/json");
        PrintWriter out = response.getWriter();
        
        
        TipoUbicacion tipoUbicacion = new TipoUbicacion();
        int id = Integer.parseInt(request.getParameter("idTipoUbicacion"));
        String nombre = request.getParameter("nombre");
        String descripcion = request.getParameter("descripcion");
        
        tipoUbicacion.setIdTipoUbicacion(id);
        tipoUbicacion.setDescripcion(descripcion);
        tipoUbicacion.setNombre(nombre);
        
        //--Se devuelve el json 
        JsonFormat json = new JsonFormat();

        if (DaoTipoUbicacion.sqlUpdate(tipoUbicacion)) {
            
            json.addField("codigo", 1);
        } else {
            json.addField("codigo", 0);
        }

    }
    public void deleteTipoUbicacion(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("application/json");
        PrintWriter out = response.getWriter();
        
        
        TipoUbicacion tipoUbicacion = new TipoUbicacion();
        int id = Integer.parseInt(request.getParameter("idTipoUbicacion"));
        
        
        tipoUbicacion.setIdTipoUbicacion(id);
       
        //--Se devuelve el json 
        JsonFormat json = new JsonFormat();

        if (DaoTipoUbicacion.sqlDelete(tipoUbicacion)) {
            
            json.addField("codigo", 1);
        } else {
            json.addField("codigo", 0);
        }

    }
    public void selectAll(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("application/json");
        PrintWriter out = response.getWriter();
        
        
        TipoUbicacion tipoUbicacion = new TipoUbicacion();
        
        ArrayList<TipoUbicacion> tipoUbicaciones = DaoTipoUbicacion.sqlSelectAll();
        
        
        //--Se devuelve el json 
        
        if(tipoUbicaciones != null)
        {
            Gson json = new Gson();
            
            out.write(json.toJson(tipoUbicaciones));
        }
        

    }
}
